package com.example.demo.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.example.demo.entity.Banner;
import com.example.demo.service.IBannerService;
import com.example.demo.utils.PageResult;
import com.example.demo.utils.PassToken;
import com.example.demo.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * bannerController
 * 
 * @author ruoyi
 * @date 2025-03-27
 */
@RestController
@RequestMapping("/system/banner")
public class BannerController
{
    @Autowired
    private IBannerService bannerService;

    /**
     * 查询banner列表
     */
    @PassToken
    @GetMapping("/list")
    public ResponseResult list(Banner banner, @RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "3") int pageSize)
    {
        PageResult pageResult= bannerService.selectBannersList(banner, pageNum, pageSize);
//        startPage();

        return ResponseResult.createBySuccess(200,pageResult,"查询成功");
    }

//    /**
//     * 导出banner列表
//     */
//    @PreAuthorize("@ss.hasPermi('system:banner:export')")
//    @Log(title = "banner", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Banner banner)
//    {
//        List<Banner> list = bannerService.selectBannerList(banner);
//        ExcelUtil<Banner> util = new ExcelUtil<Banner>(Banner.class);
//        util.exportExcel(response, list, "banner数据");
//    }

    /**
     * 获取banner详细信息
     */

    @GetMapping(value = "/{id}")
    public  ResponseResult getInfo(@PathVariable("id") Long id)
    {
        Banner banner= bannerService.selectBannerById(id);
        return  ResponseResult.createBySuccess(200,banner,"查询成功");
    }

    /**
     * 新增banner
     */

    @PostMapping
    public  ResponseResult add(@RequestBody Banner banner)
    {

        int i=bannerService.insertBanner(banner);
        if(i>0){
            return ResponseResult.createBySuccess(200,"","添加成功");
        }else {
            return ResponseResult.createByFail(500, "添加失败");
        }
    }

    /**
     * 修改banner
     */

    @PutMapping
    public ResponseResult edit(@RequestBody Banner banner)
    {

        int i=bannerService.updateBanner(banner);
        if(i>0){
            return ResponseResult.createBySuccess(200,"","更新成功");
        }else {
            return ResponseResult.createByFail(500, "更新失败");
        }
    }

    /**
     * 删除banner
     */
	@DeleteMapping("/{ids}")
    public ResponseResult remove(@PathVariable Long[] ids)  //批量删除，单个 删除
    {
        int i=bannerService.deleteBannerByIds(ids);
        if(i>0){
            return ResponseResult.createBySuccess(200,"","删除成功");
        }else {
            return ResponseResult.createByFail(500, "删除失败");
        }
    }
}
